package cn.baimo.controller;

import cn.baimo.mapper.UserInfoMapper;
import cn.baimo.mapper.UserMapper;
import cn.baimo.pojo.User;
import cn.baimo.pojo.UserInfo;
import cn.baimo.tools.DateTools;
import cn.baimo.tools.NumberTools;
import cn.baimo.tools.SMSUtil;
import cn.baimo.tools.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: UserController
 * Description:
 * date: 2020/8/8 0008 19:20:11
 *
 * @author BaiMo
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * Description: 进入主界面
     *
     * @param
     * @return
     * @author: BaiMo
     * @date: 2020/8/24 0024 17:33:01
     */
    @RequestMapping("/toLoginPage")
    public String toLoginPage() {
        System.out.println("进入登录页……");
        return "sign-in";
    }

    /**
     * Description: 登录方法
     *
     * @param
     * @return
     * @author: BaiMo
     * @date: 2020/8/24 0024 17:04:36
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user, Model model, HttpSession session) {
        System.out.println("登录中……" + user.getUid() + "——" + user.getPwd());
        user.setPwd(StringTools.str2MD5(user.getPwd()));
        User loginUser = userMapper.selectUserByUidAndPwd(user);
        if (loginUser != null) {
            System.out.println(loginUser.getName() + "登录成功");
            session.setAttribute("loginUser", loginUser);
            return "user-index";
        }
        System.err.println(user.getUid() + "登录失败");
        model.addAttribute("msg", "密码错误！请检查大小写");
        return "sign-in";
    }

    /**
     * Description: 发送验证码
     *
     * @param
     * @return
     * @author: BaiMo
     * @date: 2020/8/24 0024 18:08:20
     */
    @RequestMapping("/senCode")
    @ResponseBody
    public String senCode(String phone, HttpSession session) {
        System.out.println("发送验证码...");
        String checkCode = NumberTools.getRandom(100000, 999999) + "";
        int i = SMSUtil.senMsg(phone, checkCode);
        if (i > 0) {
            stringRedisTemplate.opsForValue().set("checkCode" + session.getId(), checkCode, 300, TimeUnit.SECONDS);
            return "短信发送成功！";
        }
        return "短信发送失败！";
    }

    /**
     * Description: 注册
     *
     * @param
     * @return
     * @author: BaiMo
     * @date: 2020/8/24 0024 17:04:09
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(User user, Model model, HttpSession session, String check) {
        System.out.println("注册中……" + user);
        String msg2 = "";
        String s = stringRedisTemplate.opsForValue().get("checkCode" + session.getId());
        if (s != null && !s.equals(check)) {
            model.addAttribute("msg2", "验证码错误或过期");
            return "sign-in";
        }
        user.setPwd(StringTools.str2MD5(user.getPwd()));
        user.setRegDataTime(DateTools.getDateTime());
        Integer integer = userMapper.insertUser(user);
        if (integer == 1) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUser(user);
            userInfo.setName(user.getName());
            userInfo.setRegDataTime(user.getRegDataTime());
            Integer info = userInfoMapper.insertUserInfo(userInfo);
            if (info == 1) {
                model.addAttribute("msg2", "注册成功");
            } else {
                model.addAttribute("msg2", "注册成功，但信息添加失败");
            }
            return "sign-in";
        }
        model.addAttribute("msg2", "注册失败!");
        return "sign-in";
    }

    /**
     * Description: 退出登录
     *
     * @param
     * @return
     * @author: BaiMo
     * @date: 2020/8/24 0024 21:08:20
     */
    @RequestMapping(value = "/exit", method = RequestMethod.POST)
    public String exit(HttpSession session) {
        System.out.println("退出登录");
        session.removeAttribute("loginUser");
        return "forward:index";
    }
}
